﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WinForm {
    public partial class FormContactDetail : Form {
        protected int id;
        BLL.Contact bll = new BLL.Contact();
        public FormContactDetail(int id) {
            this.id = id;
            InitializeComponent();
        }

        private void FormContactDetail_Load(object sender, EventArgs e) {
            FillGroup();
            Model.Contact model = new Model.Contact();
            model = bll.GetModel(id);
            if (model == null) {
                MessageBox.Show("获取数据异常。");
                return;
            }
            else {
                txtName.Text = model.Name;
                txtPhone.Text = model.Phone;
                txtQQ.Text = model.QQ;
                txtEmail.Text = model.Email;
                txtWorkPhone.Text = model.OfficePhone;
                txtWorkAddress.Text = model.OfficeAddress;
                txtHomePhone.Text = model.HomePhone;
                txtHomeAddress.Text = model.HomeAddress;
                txtMemo.Text = model.Memo;
                cboGroup.SelectedValue = model.GroupId;
            }
        }

        protected void FillGroup() {
            BLL.ContactGroup group = new BLL.ContactGroup();
            cboGroup.DisplayMember = "groupname";
            cboGroup.ValueMember = "id";
            cboGroup.DataSource = group.GetList("");
        }

        private void btnClose_Click(object sender, EventArgs e) {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e) {
            Model.Contact model = new Model.Contact();
            model.Name = txtName.Text.Trim();
            model.Phone = txtPhone.Text.Trim();
            model.Email = txtEmail.Text.Trim();
            model.QQ = txtQQ.Text.Trim();
            model.OfficeAddress = txtWorkAddress.Text.Trim();
            model.OfficePhone = txtWorkPhone.Text.Trim();
            model.HomeAddress = txtHomeAddress.Text.Trim();
            model.HomePhone = txtHomePhone.Text.Trim();
            model.Memo = txtMemo.Text.Trim();
            model.GroupId = (int)cboGroup.SelectedValue;
            string msg = "";

            if (bll.Add(model, out msg)) {
                MessageBox.Show("更新联系人信息成功。");
            }
            else {
                MessageBox.Show(msg);
            }
            this.Close();
        }
    }
}